<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Topic extends MY_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('topic_model','topic');
		$this->load->model('reply_model','reply');
	}
	public function cat(){
	    echo $this->load->view('ajax_cat',$this->_data,true);
	}
	
	public function all() {

	    $this->check_page_login();
	    
	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
		$this->load->model('topic_model');
		$list_topic = $this->topic_model->get_list_by_page($limit_num,$this->p);
		$loginer_id = empty($this->loginer->id) ? 0 : $this->loginer->id;
		$list_topic = $this->topic_model->list_favo_status($list_topic,$loginer_id);
		$this->_data['list_topic'] = $list_topic;
		
	    //分页导航
    	$this->pagination->base_url = $this->page_url($this->p);;
	    $this->pagination->total_rows = $this->topic_model->count;
	    $this->_data['pagelinks'] = $this->pagination->create_links();

	    //当前导航
	    $this->_data['nav_all'] = ' c ';
	    
    	$this->load->view('topic/all',$this->_data);

	}
	
	public function follow() {
	    
	    $this->check_page_login();

	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
		$this->load->model('topic_model');
		
		$list_topic = $this->topic_model->get_follow_by_page($limit_num,$this->loginer->id);
		$list_topic = $this->topic_model->list_favo_status($list_topic,$this->loginer->id);
		$this->_data['list_topic'] = $list_topic;
		
	    //分页导航
    	$this->pagination->base_url = $this->page_url($this->p);;
	    $this->pagination->total_rows = $this->topic_model->count;
	    $this->_data['pagelinks'] = $this->pagination->create_links();
	    
		$this->load->view('topic/follow',$this->_data);
		
	}
	
	public function tfavo() {
		$this->check_page_login();

	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
		$this->load->model('topic_model');
		$list_topic = $this->topic_model->get_favo_by_page($limit_num,$this->loginer->id);
		$list_topic = $this->topic_model->list_favo_status($list_topic,$this->loginer->id);
		$this->_data['list_topic'] = $list_topic;

		//分页导航
    	$this->pagination->base_url = $this->page_url($this->p);;
	    $this->pagination->total_rows = $this->topic_model->count;
	    $this->_data['pagelinks'] = $this->pagination->create_links();
	    
	    //当前导航
	    $this->_data['nav_favo'] = ' c ';
	    
		$this->load->view('topic/favo',$this->_data);
		
	}
	
	public function post(){
	    $this->check_ajax_login();
		echo $this->load->view('topic/post',$this->p,true);
	}
	
	public function search(){
		
		//登录检测
		$this->check_ajax_login();

		//数据检测
	    if(strlen($this->p['kws'])<=0) die;	
	    $kws = $this->p['kws'];
	    $kws = preg_replace('/\s/', '+', $kws);
		$this->p['poster_id'] = $this->loginer->id;
		$pagenum = (empty($this->p['pagenum'])) ? 1 : $this->p['pagenum'];
		
		$rs = $this->topic->get_rs_by_kws($kws,$pagenum);
		$rs->current_page = $pagenum-1;
		$this->_data['rs'] = $rs;
		
		echo $this->load->view('topic/search_rs',$this->_data,true);
		
	}
	
	function post_do(){
		
		$this->check_ajax_login();

		//数据检测
		if(empty($this->p['music_id'])) die;
		if(empty($this->p['music_name'])) die;
		if(empty($this->p['music_type'])) die;
		if(!empty($this->p['recmd']) && $this->p['recmd'] == 'yes'){
		    $this->p['type'] = 2;
		}
		$this->p['poster_id'] = $this->loginer->id;
		$topic_id = $this->cm->save('topic',$this->p);

		if(!empty($this->p['recmd']) && $this->p['recmd'] == 'yes'){
		    
		    if(empty($this->p['to_id'])) die;
		    
            $data = array();
            $data['from_id'] = $this->loginer->id;
            $data['to_id'] = $this->p['to_id'];
            $data['reason_id'] = $this->p['reason_id'];
            $data['topic_id'] = $topic_id;
            $data['atime'] = mktime();
            
            $this->cm->save('recmd',$data);
            
		}
		
		
		ejd('','','reload');
		
	}
	
	function del(){
		
		//登录检测
		$this->check_ajax_login();
		
		//数据检测
		$id = $this->p['id'];
		if(empty($id)) die;
		
		//主题是否存在
		$topic = $this->cm->get_by_id('topic',$id);
		if(empty($topic->id)) die;
		
		//只能删除自己的主题
		if($this->loginer->id != $topic->poster_id) die;
		
		//删除数据
		if($this->cm->update('topic',array('status'=>0),array('id'=>$id))){
			ejd('succ');
		}
		
		ejd('fail');
		
	}
	
	function sub_del(){
		
		//登录检测
		$this->check_ajax_login();
		
		//数据检测
		$id = $this->p['id'];
		if(empty($id)) die;
		
		//主题是否存在
		$topic = $this->cm->get_by_id('topic',$id);
		if(empty($topic->id)) die;
		
		//只能删除自己的主题
		if($this->loginer->id != $topic->poster_id) $this->page_to_index();
		
		//删除数据
		if($this->cm->update('topic',array('status'=>0),array('id'=>$id))){
			ejd('succ');
		}
		
	}
	
	function view(){
		
		if(empty($this->p['id'])) die;
		$r = $this->topic->load($this->p['id']);
		if(empty($r->id)) redirect(site_url('c=topic&m=findnone'));
		
		//收藏状态
	    $r->favo_status = 'normal';
		if($this->is_login){
		    if($this->loginer->id == $r->poster_id){
    		    $r->favo_status = 'unfavo';
		    }else{
		        if($this->cm->in($this->tbl->favo,array('tid'=>$r->id,'member_id'=>$this->loginer->id)))
		            $r->favo_status = 'favoed';
		    }
		}
		
		//收藏的人
		$this->load->model('favo_model','favo');
		$this->_data['list_favoer'] = $this->favo->get_favoer($r->id);
		
		$r->count_favo = count($this->_data['list_favoer']);
		$this->_data['count_favo'] = $r->count_favo;
		
		$this->_data['r'] = $r;
		
		//取得子主题
		$list_sub = $this->topic->get_sub($this->p['id']);
		$this->_data['list_sub'] = $list_sub;
		
		//已关注作者？
		if($this->is_login){
			$cond = array('uid'=>$this->loginer->id,'fid'=>$r->poster_id);
			$this->_data['is_followed'] = $this->cm->in('follow',$cond);
		}
		
		//取得主题回复
		$list_reply = $this->reply->get_by_page($this->p['id'],1);
		$this->_data['list_reply'] = $list_reply;
		
		//取得总回复数
		$this->_data['count_reply'] = $this->cm->count($this->tbl_reply,array('tid'=>$this->p['id'],'is_del'=>0));

		//喜欢的人
		$this->load->model('dig_model','dig');
		$this->_data['list_good_diger'] = $this->dig->get_good_diger($r->id);
		$this->_data['count_good_diger'] = count($this->_data['list_good_diger']);
		
		$this->load->view('topic/view',$this->_data);
		
	}
	
	function addsub(){
		
		//登录检测
		$this->check_page_login();
		
		$id = $this->p['id'];
		if(empty($id)) die;

		//主题是否存在
		$topic = $this->cm->get_by_id('topic',$id);
		if(empty($topic->id)) die;
		
		//只能为自己的主题添加子主题
		if($this->loginer->id != $topic->poster_id) $this->page_to_index();
		
		$this->_data['pid'] = $id;
		
		$this->load->view('topic/post',$this->_data);
	}
	
	function media(){
		if(empty($this->p['id'])) die;		
		$r = $this->cm->get_by_id($this->tbl_topic,$this->p['id']);
		$this->_data['media'] = $r->media;
		echo $this->load->view('topic/ajax_media',$this->_data,true);
	}
	
	function dig(){
		$this->check_ajax_login();
		if(empty($this->p['id'])) die;
		$id = $this->p['id'];
		$type = (empty($this->p['type']) || !in_array($this->p['type'], array('good','bad'))) ? 'good' : $this->p['type'];
		$topic = $this->topic->get_by_id($this->p['id']);	
		$count = $topic->count_good_dig;
		if(empty($topic->id)) die;
		
		//不能投自己的票
		if($topic->poster_id == $this->loginer->id) 
			ejd('fail','不能给自己投票！');
			
		//不能重复投票
		if($this->cm->in($this->tbl_dig,array('tid'=>$id,'poster_id'=>$this->loginer->id)))
			ejd('fail','不能重复投票！');
		
		$data = array();
		$data['tid'] = $this->p['id'];
		$data['poster_id'] = $this->loginer->id;
		$data['type'] = $type;
		$this->cm->insert($this->tbl_dig,$data);
		
		//更新主题（好投票数+1，优数加一）
		$sql = "update topic set count_good_dig=count_good_dig+1,count_good=count_good+1
			where id=".$id."
		";
		$this->db->query($sql);
		$count++;
		
		ejd('succ','','',array('count'=>$count));
	}
	
	function favo(){
		$this->check_ajax_login();
		if(empty($this->p['id'])) die;
		$id = $this->p['id'];
		$topic = $this->topic->get_by_id($this->p['id']);	
		if(empty($topic->id)) die;
		$count_favo = $topic->count_favo;		
		
		//不能收藏自己的主题
		if($topic->poster_id == $this->loginer->id) 
			ejd('','不能收藏自己的主题！');
			
		//不能重复投票
		if($this->cm->in($this->tbl_favo,array('tid'=>$id,'member_id'=>$this->loginer->id))){
			$this->cm->delete($this->tbl_favo,array('tid'=>$id,'member_id'=>$this->loginer->id));
			$msg = '收藏';
			$sql = "update topic set count_favo=count_favo-1,count_good=count_good-1
				where id=".$id."
			";
			$count_favo--;
		}else{
			$this->cm->insert($this->tbl_favo,array('tid'=>$id,'member_id'=>$this->loginer->id));
			$msg = '取消收藏';
			$sql = "update topic set count_favo=count_favo+1,count_good=count_good+1
				where id=".$id."
			";
			$count_favo++;
		}
		
		//更新主题（收藏次数+1，优数加一）
		$this->db->query($sql);
		
		ejd('succ',$msg,'',array('count_favo'=>$count_favo));
	}
	
	function reply(){
	    if(empty($this->p['tid'])) die;
	    $this->_data['tid'] = $this->p['tid'];
        $this->_data['total_count_reply'] = $list = $this->reply->get_count($this->p['tid']);;
        echo $this->load->view('topic/reply',$this->_data,true);	    
	}
	
	function reply_do(){
	    if(empty($this->p['tid'])) ejd('fail','error!');
	    if(empty($this->p['cont'])) ejd('fail','内容不能为空!');
	    $this->check_ajax_login();
	    $this->p['poster_id'] = $this->loginer->id;
	    $this->cm->save($this->tbl_reply,$this->p);
	    
		//更新主题（回复数+1）
		$sql = "update topic set count_reply=count_reply+1
			where id=".$this->p['tid']."
		";
		$this->db->query($sql);
	    
	    ejd('succ','回复成功!');
	    
	}
	
	function list_reply(){
	    if(empty($this->p['tid'])) ejd('fail','error!');
	    $page = (empty($this->p['page'])) ? 1 : $this->p['page'];
	    $list = $this->reply->get_by_page($this->p['tid'],$page);
	    $this->_data['total_count_reply'] = $this->reply->count;
	    $this->_data['list'] = $list;
        echo $this->load->view('topic/list_reply',$this->_data,true);	    
	}
	
	function forward(){
		
		//登录检测
		$this->check_ajax_login();
		
		//必要参数检测
		if(empty($this->p['rid'])) die;
		
		
        echo $this->load->view('topic/forward',$this->p,true);	    
	}
	
	function forward_do(){
		//登陆检测
		$this->check_ajax_login();
		
		//参数检测
		if(empty($this->p['rid'])) die;
		
		
		//被转发主题
		$topic_forward = $this->topic->get_by_id($this->p['rid']);
		
		//保存新主题
		$data = array();
		$data['poster_id'] = $this->loginer->id;
		$data['pid'] = $this->p['rid'];
		if(!empty($topic_forward->rid)){
    		$data['rid'] = $topic_forward->rid;
		}else{
    		$data['rid'] = $this->p['rid'];
		}
		$data['music_id'] = $topic_forward->music_id;
		$data['music_name'] = $topic_forward->music_name;
		$data['cont'] = $this->p['cont'];
		$this->cm->save('topic',$data);
		
		//更新原主题（转发次数+1，优数加一）
		$sql = "update topic set count_forward=count_forward+1,count_good=count_good+1
			where id in(".$this->p['rid'].",".$topic_forward->rid.")
		";
		$this->db->query($sql);
		
		ejd('','','reload');
	}
	
	function hit(){
	    
	    if(empty($this->p['id'])) die;
	    
	    $arr_hit = $this->session->userdata('arr_hit');
	    $arr_hit = (array)$arr_hit;
	    if(!in_array($this->p['id'], $arr_hit)){
            $arr_hit[] = $this->p['id'];        
            $this->session->set_userdata('arr_hit',$arr_hit);    	        
    		$sql = "update topic set count_hit=count_hit+1
    			where id=".$this->p['id']."
    		";
    		$this->db->query($sql);
	    }

	    ejd();
	}
	
	function get_latest(){
		$scrollpage = $this->session->userdata('scrollpage');
	    
	    $this->load->model('topic_model');
		$list_topic = $this->topic_model->get_latest($scrollpage);
		$this->session->set_userdata('scrollpage',$scrollpage+1);
		
        if(empty($list_topic)){
            return '';
        }else{
    		$this->_data['list_topic'] = $list_topic;
    		echo $this->load->view('inc_index_topic_list',$this->_data);
        }		
	    
	}
	
}

